from datetime import datetime

import redis
import time

redis_client = redis.StrictRedis(host='192.168.4.17', port=30056, db=7, password='Wanji@300552!')
# redis_client = redis.StrictRedis(host='106.120.201.126', port=14611, db=0, password='Wanji@300552!')

# 定义一个订阅者函数
def subscribe_channel(channel):
    pubsub = redis_client.pubsub()
    pubsub.subscribe(channel)

    # 开始监听消息
    for message in pubsub.listen():
        if message['type'] == 'message':
            print(f"Received message: {datetime.now().strftime('%H:%M:%S'), message['data'].decode('utf-8')}")


if __name__ == "__main__":
    # 在一个线程中启动订阅者
    import threading

    subscriber_thread = threading.Thread(target=subscribe_channel, args=('admin_544_0_5_control',))
    subscriber_thread.start()
